﻿
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content;
namespace Gamev1
{
    public class Bullet : Sprite
    {
        public int power;
        private const float speed = 10f;
        private Weapon weapon;
        public Vector2 Velocity { get; set; }
        public int direction;
        public string[] animationPath;
        private Animation animation;
        private float animationDelay;
        private bool hasHitCharacter;
        private float elapsedTime;

        public Bullet(Weapon weapon)
        {
            this.weapon = weapon;
            direction = (int)weapon.direction;
            checkPosition();
            power = 13;
        }

        public Bullet(int aantalTextures, float animationDelay)
        {
            animationPath = new string[aantalTextures];
            this.animationDelay = animationDelay;
            power = 8;
        }

        public void checkPosition()
        {
            if (direction == (int)Direction.Left)
            {
                Position = new Vector2(weapon.Position.X - 10, weapon.Position.Y);
            }
            else
            {
                Position = new Vector2(weapon.Position.X + weapon.Rectangle.Width, weapon.Position.Y);
            }
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            animation.Draw(spriteBatch, this);
        }

        public override void update(GameTime gameTime)
        {
            animation.Update(gameTime, direction);
            checkBossBullets(gameTime);
            checkBounds();
            move();
            Position += Velocity;
        }

        public void checkBossBullets(GameTime gameTime)
        {
            if (hasHitCharacter && weapon == null)
            {
                elapsedTime += (float)gameTime.ElapsedGameTime.Milliseconds;
                if (elapsedTime >= animation.Frames * animationDelay)
                {
                    canBeRemoved = false;
                }
            }
        }

        public override void loadContent(ContentManager content)
        {
            animation = new Animation(animationDelay);
            animation.initializeTextures(content, animationPath);
            Texture = animation.AnimationTextures[0];
        }

        public void checkBounds()
        {
            if (atEdge())
            {
                canBeRemoved = true;
            }
            if (weapon == null)
            {
                if (animation.Frames == animation.AnimationTextures.Length)
                {
                    canBeRemoved = true;
                }
            }
        }

        private bool atEdge()
        {
            return Position.X <= 0 || Position.X >= Game1.screenWidth;
        }

        public void move()
        {
            if (direction == (int)Direction.Left)
            {
                Velocity = new Vector2(-speed, 0);
            }
            else if (direction == (int)Direction.Right)
            {
                Velocity = new Vector2(speed, 0);
            }
        }

        public override void collisionWith(Sprite sprite)
        {
            if (sprite is Enemy)
            {
                Enemy enemy = (Enemy)sprite;
                enemyCollision(enemy);
            }
            else if (sprite is Character)
            {
                if (!hasHitCharacter && weapon == null)
                {
                    Character character = (Character)sprite;
                    characterCollision(character);
                }
            }
            else if (sprite is Platform)
            {
                canBeRemoved = true;
            }
        }

        public void enemyCollision(Enemy enemy)
        {
            if (weapon != null)
            {
                enemy.healthBar.Hit(power);
                enemy.currentHealth -= power;
                canBeRemoved = true;
            }
        }

        public void characterCollision(Character character)
        {
            character.Health -= power;
            hasHitCharacter = true;
        }
    }
}
